package resource;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.util.Vector;

import javax.microedition.rms.RecordStore;
import javax.microedition.rms.RecordStoreException;

import Objects.Statistic;

public class HighScoreStore {
	public RecordStore rs;
    
    private ByteArrayOutputStream bos;
    private DataOutputStream dos;
    private ByteArrayInputStream bis;
    private DataInputStream dis;
    
    private byte[] writeByte;
    
    private static final String RECORD_STORE_NAME = "TaiXiuGame";
    private static final int NUM_RECORD           = 5;
    
    public void open() {
        try {
            rs = RecordStore.openRecordStore(RECORD_STORE_NAME, true);
            if(rs.getNumRecords() == 0) {
            	insert(new Statistic("hide",13,0));
            	insert(new Statistic("jay",13,0));
            	insert(new Statistic("trungpt2",50,3));
            	insert(new Statistic());
            	insert(new Statistic());
            }
        }
        catch(RecordStoreException re) {
            System.out.println("Can't open HighScoreStore");
            System.out.println(re.getMessage());
        }
    }
    public void close() {
        try {
            rs.closeRecordStore();
        }
        catch(NullPointerException npe) {
            System.out.println("RMS 's reference is null");
            System.out.println(npe.getMessage());
        }
        catch(RecordStoreException e) {
            System.out.println("Can't close RMS");
            System.out.println(e.getMessage());
        }
    }
    
    public void insert(final Statistic p) {
        try {
            bos = new ByteArrayOutputStream();
            dos = new DataOutputStream(bos);
                    
            dos.writeUTF( p.getName( ) );
            dos.writeInt(p.getWinCount());
            dos.writeInt(p.getLoseCount());
            dos.flush();
            
            writeByte = bos.toByteArray();
            
            rs.addRecord(writeByte, 0, writeByte.length);
        }
        catch(IOException ioe) {
            System.out.println("Can't write data to RMS");
            System.out.println(ioe.getMessage());
        }
        catch(RecordStoreException rno) {
            System.out.println("Can't wirte data to RMS becase RMS closing");
            System.out.println(rno.getMessage());
        }
    }
    
    public void replaceWith(final Statistic p){
        try {
            for(int id = 1; id <= HighScoreStore.NUM_RECORD; ++id) {
                bis = new ByteArrayInputStream(rs.getRecord(id));
                dis = new DataInputStream(bis);
                
                String name = dis.readUTF();
                int win = dis.readInt();
                int lose = dis.readInt();
                
                if( lose > p.getLoseCount() ) {
                    byte[] tbye1;
                    
                    for(int i = HighScoreStore.NUM_RECORD; i >= id+1; --i) {
                        tbye1 = rs.getRecord(i-1);
                        rs.setRecord(i, tbye1, 0, tbye1.length);
                    }
                    bos = new ByteArrayOutputStream();
                    dos = new DataOutputStream(bos);
                    
                    dos.writeUTF( p.getName( ) );
                    dos.writeInt(p.getWinCount());
                    dos.writeInt(p.getLoseCount());
            
                    dos.flush();
            
                    writeByte = bos.toByteArray();
            
                    rs.setRecord(id, writeByte, 0, writeByte.length);
                    break;
                }
            }
        }
        catch(IOException ioe) {
            System.out.println("Can't write data to RMS");
            System.out.println(ioe.getMessage());
        }
        catch(RecordStoreException rno) {
            System.out.println("Can't wirte data to RMS becase RMS closing");
            System.out.println(rno.getMessage());
        }
    }
    
    public Vector read() {
        Vector v = new Vector();
        Statistic p;
        try {
        	String name;
            int win;
            int lose;
            
            for(int id = 1; id <= HighScoreStore.NUM_RECORD; ++id) {
                bis = new ByteArrayInputStream(rs.getRecord(id));
                dis = new DataInputStream(bis);
                
                name = dis.readUTF();
                win = dis.readInt();
                lose = dis.readInt();
                
                p = new Statistic(name, win, lose);
                v.addElement(p);
            }
        }
        catch(IOException ioe) {
            System.out.println("Can't write data to HighScoreStore");
            System.out.println(ioe.getMessage());
        }
        catch(RecordStoreException rno) {
            System.out.println("Can't read data to HighScoreStore becase HighScoreStore closing");
            System.out.println(rno.getMessage());
        }
        
        return v;
    }
    
    public boolean isTop(final Statistic p) {
        boolean result = false;
        
        try {
            bis = new ByteArrayInputStream(rs.getRecord(HighScoreStore.NUM_RECORD-1));
            dis = new DataInputStream(bis);

            String name = dis.readUTF();
            int win = dis.readInt();
            int lose = dis.readInt();

            if(lose >  p.getLoseCount() ) {
                result = true;
            }
        }
        catch(RecordStoreException rse) {
            System.out.println("in is_top method");
            System.out.println(rse.getMessage());
        }
        catch(IOException ioe) {
            System.out.println("error with reading data from input stream");
            System.out.println(ioe.getMessage());
        }
        return result;
    }
}
